package com.slz3.business.generator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class CloneTemplateGenerator {

    static final Path SOURCE = Paths.get("/Users/jinxing/SD/Projects/mishang-business-angular/src/app/module/template-example/tem-style1");
    static final Path TARGET = Paths.get("/Users/jinxing/SD/Projects/mishang-business-angular/src/app/module/config-management/con-version");

    private String replaceTarToSou(String line) {
        return line.replace("TemplateExample", "ConfigManagement")
                .replace("templateExample", "configManagement")
                .replace("template_example", "config_management")
                .replace("template-example", "config-management")
                .replace("TemStyle1", "ConVersion")
                .replace("temStyle1", "conVersion")
                .replace("tem_style1", "con_version")
                .replace("tem-style1", "con-version")
                ;
    }

    public static void main(String[] args) throws Exception {
        new CloneTemplateGenerator().execute();
    }

    public boolean execute() throws Exception {
        // 1 遍历“模板根目录”下的每一个模板文件
        Files.walkFileTree(SOURCE, new CloneTemplateToTargetFileVisitor());
        return true;
    }

    /**
     * Created by jinxing on 2017/4/9.
     * // 1 遍历“模板根目录”下的每一个模板文件
     * Files.walkFileTree(file, new CloneTemplateToTargetFileVisitor());
     */
    class CloneTemplateToTargetFileVisitor extends SimpleFileVisitor<Path> {
        /**
         * 2.唯一要做的就是重写 visitFile，让它来为你工作；
         **/
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            // 3.过滤所有来源目录文件
            if (file.toString().endsWith(".java")
                    || file.toString().endsWith(".ts")
                    || file.toString().endsWith(".jsp")
                    || file.toString().endsWith(".js")
                    || file.toString().endsWith(".css")) {
                // 来源目录文件绝对路径
                String souFileAbsDir = file.toString();
                System.out.println(souFileAbsDir);
                // 目标目录文件绝对路径 // 修改跟路径 // 修改文件名
                String tarFileAbsDir = replaceTarToSou(souFileAbsDir.replace(SOURCE.toString(), TARGET.toString()));
                System.out.println(tarFileAbsDir);
                // [生成]目标目录 & 文件
                Path tarFolder = Paths.get(tarFileAbsDir.substring(0, tarFileAbsDir.lastIndexOf(File.separator)));
                Path tarFile = Paths.get(tarFileAbsDir);
                if (Files.notExists(tarFolder)) Files.createDirectories(tarFolder);
                if (Files.notExists(tarFile)) {
                    Files.createFile(tarFile);
                    // 写入
                    try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8);
                         BufferedWriter writer = Files.newBufferedWriter(tarFile, StandardCharsets.UTF_8,
                                 StandardOpenOption.WRITE)) {
                        String line;
                        while ((line = reader.readLine()) != null) {
                            writer.write(replaceTarToSou(line));
                            writer.write("\n");
                        }
                    }
                }
            }
            // 4.return
            return FileVisitResult.CONTINUE;
        }
    }

}
